<?php

namespace app\services\agent;

use app\model\agent\AgentLevel;
use app\model\agent\AgentRelation;
use app\model\user\User;
use app\services\BaseServices;
use think\facade\Db;

class FenxiaoService extends BaseServices
{

    /**
     *
     * 维护用户表种的分销状态
     * @param $user_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function fillUserAgentInfoByRegister($user_id)
    {
        $user = User::where('uid', $user_id)->where('is_del', 0)->find();
        if (!$user || $user->status == 0) return false;

        // 寻找最低一级分销  => 注册就可以成为青铜
        $agentLevel = AgentLevel::where('is_del', 0)->where('status', 1)->order('grade', 'asc')->find();
        if (!$agentLevel) return false;


        $this->transaction(function () use ($user, $agentLevel) {

            $now = time();

            $user->spread_uid = 0;
            $user->spread_open = 1;
            $user->agent_level = $agentLevel->id;
            $user->spread_time = $now;

            AgentRelation::create([
                'uid'   =>  $user->uid,
                'first_puid'    =>  0,
                'second_puid'   =>  0,
                'third_puid'    =>  0,
                'four_puid'    =>  0,
                'level' =>  $agentLevel->id,
                'add_time'  => $now
            ]);
        });

        return $user->save();

    }

    /**
     *
     * 绑定用户关系
     * @param $user_id
     * @param $spread_id
     * @return bool
     */
    public function bindUserRelation($user_id, $spread_id)
    {
        $user = User::where('uid', $user_id)->where('is_del', 0)->find();
        if (!$user || $user->status == 0 || $user->spread_uid) return false;
        $spread = User::where('uid', $spread_id)->where('is_del', 0)->find();
        if (!$spread || $spread->status == 0) return false;

        $now_agent = AgentRelation::where('uid', $user_id)->find();
        if (!$now_agent) return false;

        // 获取父亲一级的记录
        $parent_agent = AgentRelation::where('uid', $spread->uid)->find();
        if (!$parent_agent) return false;


        // 寻找最低一级分销  => 注册就可以成为青铜
//        $agentLevel = AgentLevel::where('is_del', 0)->where('status', 1)->order('grade', 'desc')->find();
//        if (!$agentLevel) return false;
        // 检查分销表情况
//        $count = AgentRelation::where('uid', $user_id)->where('first_puid', $spread_id)->count();
//        if ($count) return true;


        $getAgentLevel = function ($spread_uid) {
            // 更改介绍人的等级
            $parent_spread_info = AgentRelation::where('first_puid', $spread_uid)->column('level');
            // 统计数组中每个值的出现次数
            $counts = array_count_values($parent_spread_info);

            // 判断是否满足条件并返回对应的结果
            if (isset($counts[3]) && $counts[3] >= 5) {
                return 4;
            } elseif (isset($counts[2]) && $counts[2] >= 5) {
                return 3;
            } elseif (isset($counts[1]) && $counts[1] >= 5) {
                return 2;
            } else {
                return 1; // 如果不满足条件，返回0或其他你认为合适的值
            }
        };


        $this->transaction(function () use ($user, $spread, $getAgentLevel, $now_agent, $parent_agent) {

            $user->spread_uid = $spread->uid;
//            $user->spread_open = 1;
//            $user->agent_level = $agentLevel->id;
//            $user->spread_time = $now;
            $user->save();

//            // 获取父亲一级的记录
//            $parent_agent = AgentRelation::where('uid', $spread->uid)->find();
//
//            AgentRelation::create([
//                'uid'   =>  $user->uid,
//                'first_puid'    =>  $spread->uid,
//                'second_puid'   =>  $parent_agent ? $parent_agent->first_puid : 0,
//                'third_puid'    =>  $parent_agent ? $parent_agent->second_puid : 0,
//                'four_puid'    =>  $parent_agent ? $parent_agent->third_puid : 0,
//                'level' =>  $user->agent_level,
//                'add_time'  => $now
//            ]);

            $now_agent->first_puid = $spread->uid;
            $now_agent->second_puid = $parent_agent->first_puid;
            $now_agent->third_puid = $parent_agent->second_puid;
            $now_agent->four_puid = $parent_agent->third_puid;
            $now_agent->save();



            $level = $getAgentLevel($spread->uid);
            if ($spread && $level != $spread->agent_level) {
                $spread->agent_level = $level;
                $spread->save();

                $parent_agent->level = $level;
                $parent_agent->save();
            }
        });
        return true;
    }


    /**
     * 获取分佣记录
     * @param $order
     * @return void
     */
    public function makeRelationMoneyLogByOrder($order)
    {
        
    }
}